// 平台告警分析（最近7天内告警统计）
angular.module('panels')
    .directive("panelBomCountAlarmAnalysis", function ($http, loadConf, $timeout, $log, moment) {
        return {
            scope: {},
            link: function (scope) {
                function gradientColor(color) {
                    return {
                        radialGradient: {cx: 0.5, cy: 0.3, r: 0.7},
                        stops: [
                            [0, color],
                            [1, Highcharts.Color(color).brighten(-0.1).get('rgb')] // darken
                        ]
                    };
                }

                function getHighChartOptions(data) {
                    return {
                        chart: {
                            type: 'column'
                        },
                        title: {
                            text: ''
                        },
                        xAxis: {
                            categories: data.categories
                        },
                        yAxis: {
                            min: 0,
                            title: {
                                text: ''
                            }
                        },
                        credits: {enabled:false},
                        exporting: {enabled:false},
                        tooltip: {
                            headerFormat: '<span style="font-size:10px">{point.key}</span><table>',
                            pointFormat: '<tr><td style="color:{series.color};padding:0">{series.name}: </td>' +
                            '<td style="padding:0"><b>{point.y}个 </b></td></tr>',
                            footerFormat: '</table>',
                            shared: true,
                            useHTML: true
                        },
                        plotOptions: {
                            column: {
                                pointPadding: 0.1,
                                borderWidth: 0
                            }
                        },
                        series: data.series
                    };
                }

                scope.options = getHighChartOptions({1: [], 2: [], 3: []}, []);
                scope.panelTitle = '平台告警分析图';
                loadConf('panel-bom-count-alarm-analysis.yml').then(function (setting) {
                    var preData, timeoutId;
                    scope.$on('$destroy', function () {
                        timeoutId && $timeout.cancel(timeoutId);
                    });
                    (function updateData() {
                        var defaultParanStr='10-10,10-11,10-12,10-14,10-15,10-16,10-17,10-18';//默认需要统计的资源kbp_class
                        $http.jsonp(setting.countQueryUrl, {params: {kbpClasses: defaultParanStr}}).success(function (data) {
                            if (angular.equals(data, preData)) {
                                return false;
                            } else {
                                preData = data;
                                data = angular.copy(data);
                            }
                            // 通知更新charts
                            scope.options = getHighChartOptions(data);
                        }).then(callback, callback);
                        function callback() {
                            if (setting.updateInterval > 0) {
                                timeoutId = $timeout(updateData, setting.updateInterval * 1000);
                            }
                        }
                        $log.debug('panel update data: bom-count-alarm-analysis');
                    }());
                });
            },
            templateUrl: 'app/panel/count-alarm-analysis-highcharts.html'
        };
    });